﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UNDP_Projects.Data_Access;

namespace UNDP_Projects.CO.Edit
{
    public partial class AddSubOutcome : System.Web.UI.Page
    {
        UNDPEntities db = new UNDPEntities();

        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("Project"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("Project") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string ID = SubOutcomeIDTextBox.Text.Trim();

                var ASubOutcome = (from s in db.sub_outcomes where s.sub_outcome_id == ID select s).FirstOrDefault();

                if (ASubOutcome == null)
                {
                    //===============================+
                    //  Add a new Outcome Record     |
                    //===============================+
                    sub_outcomes newSubOutcome = new sub_outcomes();
                    newSubOutcome.sub_outcome_id = ID;
                    newSubOutcome.outcome_id = long.Parse(OutcomeList.SelectedValue.Trim());
                    newSubOutcome.sub_outcome = SubOutcomeTextBox.Text.Trim();

                    db.sub_outcomes.AddObject(newSubOutcome);
                    db.SaveChanges();

                    error.Text = "SAVED";
                    error.Visible = true;

                    SubOutcomeIDTextBox.Text = null;
                    SubOutcomeTextBox.Text = null;
                    OutcomeList.SelectedValue = null;
                }
                else
                {
                    error.Text = "That Sub Outcome ID already exists";
                    error.Visible = true;
                }
            }
            catch (Exception ex)
            {
                error.Text = "Database Error: " + ex.Message + "<br /> " + ex.StackTrace;
                error.Visible = true;
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CO/Suboutcomes.aspx", true);
        }
    }
}